$(document).ready(function(){
	layoutLoad("info");
	
	$("#user_add > li > a").each(function( index ) {
		$(this).on("click", function(event){
			layoutLoad($(this).attr("id"));
		});
	});
	
	$(".btn_close").on("click", function(event) {
		window.close();
	});
	$(".btn_cancle").on("click", function(event) {
		window.close();
	});
	
	
	$(".btn_save").on("click", function(event) {
		var url = "insertUserAdd.jsp";
		var serviceName = "USER_ADD";
		var param = {
				insert_group : $("#insert_group").val(),
				user : $("#user").val(),
				name : $("#name").val(),
				pw : $("#pw").val(),
				pw_check : $("#pw_check").val(),
				or_name : $("#or_name").val(),
				dep_name : $("#dep_name").val(),
				position : $("#position").val(),
				email : $("#email").val(),
				handphone : $("#handphone").val(),
				ip_addr : $("#ip_addr").val(),
				target_list : $("#target_list").val(),
				req_account : $("#req_account").val(),
				use_purpose : $("#use_purpose").val(),
				use_date : $("#use_date").val(),
				use_time : $("#use_time option:selected").val()
		};
		callAjax(url,param, serviceName);
	});
});

function layoutLoad(id) {
	var url = getUrl()+"user_add_"+id+".jsp";
	$(".layer_cont").load(url, function() {
		$("#user_add > li > a").each(function( index ) {
			var eachId = $(this).attr("id");
			if( id == eachId ) {
				$(this).addClass("on");
			} else {
				$(this).removeClass("on");
			}
		});
				
		if( id == "system_choice" ) {
			loadTree("system");
		}
		
		$(document).ready(function(){
			/* ID 중복체크 */
			$(".btn_overlapchk").on("click", function(event) {
				var url = "checkId.jsp";
				var serviceName = "USER_CHECKID";
				var param = {
						user : $("#user").val()
				};
				callAjax(url,param, serviceName);
			});
		});
		
		/* info + 클릭시 */
		$("#target_add").on("click", function(event) {
			var url = "insertTarget.jsp";
			var serviceName = "TARGET_ADD";
			var param = {
					target_list : $("#target_list").val()
			};
			callAjax(url,param, serviceName);
		});
		
		
	});
}

/* callAjax successAjax */
function successAjax(data, serviceName){
	if(serviceName == "USER_ADD"){
		alert(data.result);
	}else if(serviceName == "USER_ADD_TREE"){
		
	}else if(serviceName == "USER_CHECKID"){
		alert(data.result);
	}else if(serviceName == "TARGET_ADD"){
		alert(data.result);
	}
}